<?php

namespace app\api\controller;


use app\common\controller\ApiController;
use think\facade\Db;

class DataBase extends ApiController
{ 
    protected $noNeedLogin = [];
    protected $mustLogin = [];

    //数据源管理
    public function getDataSourceList()
    {
        $dataSourceList = Db::name('jsyc_database')->where(['delete_time' => 0])->order('create_time', 'desc')->select();
        //总数据量
        $total = Db::name('jsyc_database')->where(['delete_time' => 0])->count();
        //今日新增
        $todayAdd = Db::name('jsyc_database')->where(['delete_time' => 0])->whereTime('create_time', 'today')->count();

        $this->success('获取成功', ['total' => $total, 'todayAdd' => $todayAdd, 'dataSourceList' => $dataSourceList]);
    }

    //保存数据源
    public function saveDataSource()
    {
        $data = $this->request->param();
        if (empty($data['records'])){
            $this->error('数据不能为空');
        }
        $records = $data['records'];
        foreach ($records as $record){
          //判断是否存在
          $exist = Db::name('jsyc_database')->where(['originId' => $record['id'], 'delete_time' => 0])->find();
          if ($exist){ 
            $updateParams = [
                'originalName' => $record['originalName'],
                'physicalPath' => $record['physicalPath'],
                'fileType' => $record['fileType'],
                'update_time' => time(),
            ];
            Db::name('jsyc_database')->where(['originId' => $record['id'], 'delete_time' => 0])->update($updateParams);
          }else{ 
            $insertParams = [
                'originalName' => $record['originalName'],
                'physicalPath' => $record['physicalPath'],
                'fileType' => $record['fileType'],
                'originId' => $record['id'],
                'create_time' => time(),
                'update_time' => time(),
            ];
            Db::name('jsyc_database')->insert($insertParams);
          }
        } 
        $this->success('保存成功', $data);
    }

    //更新详情
    public function updateDataSourceDetail(){
        $data = $this->request->param();
        if (empty($data['id'])){
            $this->error('数据不能为空');
        }
        if (empty($data['projectInfo'])){
            $this->error('数据不能为空');
        }
        $id = $data['id'];
        $projectInfo = $data['projectInfo'];

        $exit = Db::name('jsyc_database')->where(['originId' => $id, 'delete_time' => 0])->find();
        if (!$exit){
            $this->error('数据源不存在');
        }
        $updateParams = [
            'originalName' => $projectInfo['originalName'],
            'physicalPath' => $projectInfo['physicalPath'],
            'fileType' => $projectInfo['fileType'],
            'source_data' => $projectInfo['source_data'],
            'update_time' => time(),
        ];
        Db::name('jsyc_database')->where(['originId' => $id, 'delete_time' => 0])->update($updateParams);
        //添加同步记录
        $insertParams = [
            'databaseId' => $exit['id'],
            'originalName' => $projectInfo['originalName'],
            'physicalPath' => $projectInfo['physicalPath'],
            'fileType' => $projectInfo['fileType'],
            'source_data' => $projectInfo['source_data'],
            'create_time' => time(),
            'update_time' => time(),
        ];
        Db::name('jsyc_database_sync_record')->insert($insertParams);
        $this->success('更新成功', $data);
    }
}
